package jml.defaultimple;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;

import jml.config.Config;
import jml.config.ReservedAtts;
import jml.def.Title;
import jml.defaultimple.util.UFile;

public class TitleImple implements Title
{
	public String id;
	private String path;
	private ArrayList<String> subfolders;
	public Hashtable<String, ArrayList<String>> filters;
	
	@Override
	public boolean equals(Object o)
	{
		TitleImple other = (TitleImple)o;
		return path.equals(other.path);
	}
	
	public TitleImple()
	{
		subfolders = new ArrayList<String>();
		filters = new Hashtable<String, ArrayList<String>>();
	}
	
	public void setPath(String p)
	{
		this.path = p;
	}
	
	@Override
	public String getPath()
	{
		return path;
	}

	@Override
	public boolean isMultipleAlbum()
	{
		return !subfolders.isEmpty();
	}
	
	@Override
	public String[] getLabels(String filterName)
	{
		ReservedAtts ra = Config.getConfig(false).getReservedAtts();
		
		if( filterName.equals(ra.getIdAtt()) )
		{
			return new String[]{getId()};
		}
		
		ArrayList<String> labels = filters.get(filterName);
		
		if( labels==null )
		{
			return new String[0];
		}
		
		String slabels[] = new String[labels.size()];
		return labels.toArray(slabels);
	}

	@Override
	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id=id;
	}
	
	@Override
	public String[] getSubfolders()
	{
		String sfolders[] = new String[subfolders.size()];
		return subfolders.toArray(sfolders);
	}
	
	public void addSubfolder(String f)
	{
		subfolders.add(f);
	}
	
	public void addLabel(String filter,String label)
	{
		ArrayList<String> labels = filters.get(filter);
		if( labels==null )
		{
			labels = new ArrayList<String>();
			filters.put(filter, labels);
		}
		
		labels.add(label);
	}

	@Override
	public String toString()
	{
		String s = "Tittle: [id=" + id + ", path=" + path + ", subfolders=" + subfolders + "]\n";		
		return s;
	}

	@Override
	public File[] getFiles(String fileType)
	{
		String[] files = UFile.getFiles(path, fileType);
		File[] ret = new File[files.length];
		for(int i=0; i<files.length; i++)
		{
			ret[i] = new File(files[i]);
		}
		
		return ret;
	}	

	@Override
	public void storeCookie(String name, String value)
	{
		// si no exite => creo la carpeta
		UFile.createFolderIfNotExists(getCookieFolder());
		
		// grabo el cookie
		UFile.writeFile(getCookieFolder()+"/"+name, value);
	}

	@Override
	public String loadCookie(String name)
	{
		// si no exite => creo la carpeta
		UFile.createFolderIfNotExists(getCookieFolder());
		
		// leo el cookie
		return UFile.readFile(getCookieFolder()+"/"+name);
	}
	
	@Override
	public void removeCookie(String name)
	{		
		// si no exite => creo la carpeta
		UFile.removeFile(getCookieFolder()+"/"+name);
	}

	private String getCookieFolder()
	{
		return getPath()+"/"+Config.getConfig(false).getLibrary().getCookiesFolder();
	}

	private String getPrivateCookieFolder(Class<?> owner)
	{
		String cookieFolder = getPath()+"/"+Config.getConfig(false).getLibrary().getCookiesFolder();
		String privateFolder = owner.getName();
		return cookieFolder+"/"+privateFolder;
	}	
	
	@Override
	public void storePrivateCookie(String name, String value, Class<?> owner)
	{
		// si no exite => creo la carpeta
		UFile.createFolderIfNotExists(getPrivateCookieFolder(owner));
		
		// grabo el cookie
		UFile.writeFile(getPrivateCookieFolder(owner)+"/"+name, value);
	}

	@Override
	public String loadPrivateCookie(String name, Class<?> owner)
	{
		// si no exite => creo la carpeta
		UFile.createFolderIfNotExists(getPrivateCookieFolder(owner));
		
		// leo el cookie
		return UFile.readFile(getPrivateCookieFolder(owner)+"/"+name);	}

	@Override
	public void removePrivateCookie(String name, Class<?> owner)
	{
		// si no exite => creo la carpeta
		UFile.removeFile(getPrivateCookieFolder(owner)+"/"+name);	
	}
}



